package sdfi.lee.Entity;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.TokenHelper;

import sdfi.lee.DAO.UserDAO;
import sdfi.lee.Entity.base.BaseUser;



public class User extends BaseUser {
	private static final long serialVersionUID = 1L;
	private String tip;
	private String date;
	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	/*[CONSTRUCTOR MARKER BEGIN]*/
	public User () {
		super();
	}

	/**
	 * Constructor for primary key
	 */
	public User (java.lang.Integer id) {
		super(id);
	}
	public User(Integer id,String company,String linkman,String username,Integer ustatus,Date rgdatetime)
	{
		this.setId(id);
		this.setCompany(company);
		this.setLinkman(linkman);
		this.setUsername(username);
		this.setUstatus(ustatus);
		this.setRgdatetime(rgdatetime);
	}
	public String execute()
	{
		this.setRgdatetime(new Date());
		this.setUstatus(0);
		String flag= new UserDAO().register(this,ServletActionContext.getRequest().getSession());
		if(flag.equals("error"))
		{
			tip="注册失败！";
		}
		return flag;
	}
	public String exit()
	{	
		//System.out.println("adsfasdf");
		return new UserDAO().userexit(ServletActionContext.getRequest().getSession());
		
	}
	public String login() throws Exception 
	{
		
		this.setUsername(java.net.URLDecoder.decode(this.getUsername(),"utf-8"));
		
		String flag=new UserDAO().login(this,ServletActionContext.getRequest().getSession());
		if(flag.equals("error"))
		{
			tip="密码或是用户名错误";
		}
		return flag;
		
	}
	public String modify() throws Exception
	{	
		
		this.setId(Integer.valueOf(ServletActionContext.getRequest().getSession().getAttribute("userid").toString()));
		//System.out.println("action"+this.getUsername());
		String flag=new UserDAO().modify(this);
		if(flag.equals("error"))
		{
			tip="修改失败";
		}
		return flag;
	}
	
	public String userAdd()
	{
		try
		{
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String date=sdf.format(new Date());
			this.setDate(date);
			if("success".equals(new UserDAO().userAdd(this)))
			{
				tip="添加成功";
				return "success";
			}
			else
			{
				tip="添加失败";
				return "error";
			}
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
			return "error";
		}
	}
/*[CONSTRUCTOR MARKER END]*/


}